package com.wynn.controller;


import com.wynn.pojo.Result;
import com.wynn.serviceFeign.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("sysUser")
public class UserController {

    @Autowired
    private UserClient userClient;

//    @PostMapping("login")
//    @ResponseBody
//    public Result loginIndex(@RequestBody User user, HttpServletRequest request){
//        // 获取访问ip地址
//        String remoteAddr = request.getRemoteAddr();
//        String forwarded = request.getHeader("X-Forwarded-For");
//        String realIp = request.getHeader("X-Real-IP");
//        String visitorIp = IpUtils.getVisitorIp(remoteAddr, forwarded, realIp);
//
//        Result result = userClient.findByUser(user,visitorIp);
//        request.getSession().setAttribute("user", result.getData());
//
//        return result;
//    }
    @GetMapping("perm")
    public Result perm(Principal principal){
        Result result = new Result();
        try {
            List<String> perms = userClient.findByPerm(principal.getName());
            result.setData(perms);
            result.setCode("200");
        }catch (Exception e){
            result.setCode("500");
            result.setMessage("查找权限失败");
        }
        return result;
    }

    @PostMapping("addNotice")
    public Result addNotice(@RequestBody Map<String,String> notices){
        Result result = new Result();
        try {
            userClient.addNotice(notices);
            result.setCode("200");
            result.setMessage("添加通知成功");
        }catch (Exception e){
            result.setCode("503");
            result.setMessage("添加通知失败");
        }
        return result;

    }

    @GetMapping("selectNotice")
    public Result selectNotice(){
        Result result = new Result();
        try {
            List<Map<String,String>> notices =  userClient.selectNotice();
            result.setCode("200");
            result.setData(notices);
        }catch (Exception e){
            result.setCode("503");
            result.setMessage("加载通知失败");
        }
        return result;

    }


}
